
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.wave {

public final class ParticipantId {
	private var address:String;
	private var gender:String;
	/**
	 * Constructs an id
	 */
	public function ParticipantId(address:String, gender:String="Male") {
		if (address == null) {
			throw new Error("Null Address in Participant ID");
		}
		this.gender = gender;
		this.address = normalize(address);
	}
	
	/**
	 * Normalises an address.
	 */
	private static function normalize(address:String):String {
		if (address == null) {
			return null;
		}
		return address.toLowerCase();
	}
	
	/**
	 * @return the participant's address
	 */
	public function getAddress():String {
		return address;
	}
	
	/**
	 * @return the participant's address
	 */
	public function getGender():String {
		return gender;
	}
	
	/**
	 * @return the domain name in the address. If no "@" occurs, it will be the whole string,
	 *     if more than one occurs, it will be the part after the last "@".
	 */
	public function getDomain():String {
		var parts:Array= address.split("@");
		return parts[parts.length - 1];
	}
	
	public function equals(o:Object):Boolean {
		if (o == this) {
			return true;
		} else if (o instanceof ParticipantId) {
			var p:ParticipantId= ParticipantId(o);
			return address == (p.address);
		}
		return false;
	}
	
	public function toString():String {
		return getAddress();
	}
}
}